<?php

namespace App\Services;


use App\Models\CourseSectionWork;
use App\Models\StudentWork;

class CourseSectionWorkService extends BaseService
{
    /**
     * 获取列表
     * @return
     */
    public function list($sort,$order,$school_id,$class_grades_id,$course_id, $course_section_id, $name) {
        $query = CourseSectionWork::query();
        if(!empty($name)){
            $query->where('name','like',"%$name%");
        }
        if(!empty($course_id)){
            $query->where('course_id',$course_id);
        }
        if(!empty($course_section_id)){
            $query->where('course_section_id',$course_section_id);
        }
        if(!empty($school_id)){
            $query->where('school_id',$school_id);
        }
        if(!empty($class_grades_id)){
            $query->where('class_grades_id',$class_grades_id);
        }
        return $query->orderBy($sort,$order)->get();
    }
    /**
     * 获取列表
     * @return
     */
    public function listStudentWork($sort,$order,$users_id, $course_section_id) {
        $query = StudentWork::query();
        if(!empty($users_id)){
            $query->where('users_id',$users_id);
        }
        if(!empty($course_section_id)){
            $query->where('course_section_id',$course_section_id);
        }
        return $query->orderBy($sort,$order)->get();
    }

    /**
     * 根据章节已添加作业数量
     * @param $course_section_id
     * @param $course_id
     * @return User|null|Model
     */
    public function getSectionWorkNum($course_id,$course_section_id){
        return StudentWork::query()->where('course_id',$course_id)->where('course_section_id',$course_section_id)
            ->where('work_file','!=','')
            ->distinct('users_id')->count();
    }
}
